package com.gwsdk.paysdk.acitvityV2;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.Window;
import android.view.WindowManager;

import com.gwsdk.paysdk.utils.ToastUtil;
import com.gwsdk.paysdk.common.Constant;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import static com.tencent.mm.opensdk.modelmsg.SendMessageToWX.Req.WXSceneSession;
import static com.tencent.mm.opensdk.modelmsg.SendMessageToWX.Req.WXSceneTimeline;

/**
 * 描述：微信分享Activity
 * 作者：闫冰
 * 时间: 2017-09-22 15:35
 */
public class GWWeChatShareActivity extends Activity {
    private Boolean toFriend;
    private boolean Plug;
    private static final String TAG = "WeChatShareActivity";
    private ProgressDialog dialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        toFriend = getIntent().getBooleanExtra("toFriend",true);
        Plug = getIntent().getBooleanExtra("Plug",false);

        if(!Constant.QQ_appid.equals("")&&!Constant.WX_appid.equals("")){
            if(Plug){
                //插件版调用
                if (toFriend){
//                    wechatShare(0);  //0:分享到微信好友
                }else {
//                    wechatShare(1);  //1：分享到微信朋友圈
                }
            }else{
                //非插件版调用
                if (toFriend){
                    wechatShare2(0);  //0:分享到微信好友
                }else {
                    wechatShare2(1);  //1：分享到微信朋友圈
                }
            }
        }else{
            ToastUtil.show(this,"参数为空 无法分享");
            finish();
        }

    }

//    /**
//     * 分享至微信好友
//     * @param type (0:分享到微信好友，1：分享到微信朋友圈)
//     */
//    private void wechatShare(int type){
//        IWXAPI wxApi = WXAPIFactory.createWXAPI(this, ScreenshotListener.WX_appid);
//        wxApi.registerApp(ScreenshotListener.WX_appid);
////        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), GWInflaterUtils.getIdByName(this, "drawable", "ic_launcher"));
//        Bitmap bitmap = BitmapFactory.decodeFile(ScreenshotListener.ImgPath);
//        WXImageObject webpage = new WXImageObject(bitmap);
//        WXMediaMessage msg = new WXMediaMessage(webpage);
//        msg.mediaObject = webpage;
//        Bitmap thumb = Bitmap.createScaledBitmap(bitmap,100,100,true);
//        bitmap.recycle();
//        msg.thumbData = Util.bmpToByteArray(thumb,true);
//        SendMessageToWX.Req req = new SendMessageToWX.Req();
//        req.transaction = String.valueOf(System.currentTimeMillis());
//        req.message = msg;
//        switch (type) {
//            case 0:
//                req.scene = WXSceneSession;
//                break;
//            case 1:
//                req.scene = WXSceneTimeline;
//                break;
//        }
//        wxApi.sendReq(req);
//        finish();
////        IWXAPI wxApi = WXAPIFactory.createWXAPI(this, ScreenshotListener.WX_appid);
////        wxApi.registerApp(ScreenshotListener.WX_appid);
////
////        WXWebpageObject webpage = new WXWebpageObject();
////        webpage.webpageUrl = "http://www.baidu.com";
////        WXMediaMessage msg = new WXMediaMessage(webpage);
////        msg.title = "haha";
////        msg.description = "hehehehehehehehe";
////        msg.setThumbImage(bitmap);
////        SendMessageToWX.Req req = new SendMessageToWX.Req();
////        req.transaction = String.valueOf(System.currentTimeMillis());
////        req.message = msg;
////        switch (type) {
////            case 0:
////                req.scene = WXSceneSession;
////                break;
////            case 1:
////                req.scene = WXSceneTimeline;
////                break;
////        }
////        wxApi.sendReq(req);
////        finish();
//    }



    /**
     * 分享至微信好友
     * @param type (0:分享到微信好友，1：分享到微信朋友圈)
     */
    private void wechatShare2(int type){
        IWXAPI wxApi = WXAPIFactory.createWXAPI(this,  Constant.WX_appid);
        wxApi.registerApp( Constant.WX_appid);

        WXWebpageObject webpage = new WXWebpageObject();
        webpage.webpageUrl = Constant.ShareUrl;
        WXMediaMessage msg = new WXMediaMessage(webpage);
        msg.title = Constant.ShareTitle;
        msg.description = Constant.ShareZY;
        msg.setThumbImage( Constant.ShareBitmap);
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = String.valueOf(System.currentTimeMillis());
        req.message = msg;
        switch (type) {
            case 0:
                req.scene = WXSceneSession;
                break;
            case 1:
                req.scene = WXSceneTimeline;
                break;
        }
        wxApi.sendReq(req);
        finish();
    }
}
